﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_PhanQuyenTaiKhoan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            btnclose.Attributes.Add("onClick", "javascript:history.back(); return false;");//them su kien vao  button de quay ve trang truoc
            BindGrid();
        }
    }
    private void BindGrid()
    {
        SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        string userid = _ps.Decrypt(Request.QueryString["userid"].Replace(" ", "+"), "gold", 128);//user can phan quyen
        int userparrent = MemberShip_ASP.GetUserIDOfUserLogIn(this.Page.User.Identity.Name);//lay userid cua tai khoan cha
        //var rolelist = _db.tblUserInRoles.Where(r=>r.UserID==int.Parse(userid) && r.RolesID!=1);
        int lastroleid = _db.tblRoles.Max(r=>r.ID); //khong lay role dang nhap
        //var rolelist = from p in _db.tblUserInRoles where p.UserID == int.Parse(userid) && p.RolesID!=lastroleid  select new { ID = p.ID, Description =p.tblRole.Description.Trim(),Grant=p.PhanQuyen};
        var rolelist = _db.getrole(int.Parse(userid), userparrent, lastroleid);
        grid_news.DataSource = rolelist.ToList();
        grid_news.DataBind();
    }
    protected void linkGrant_Click(object sender, EventArgs e)
    {
        SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        string userid=_ps.Decrypt(Request.QueryString["userid"].Replace(" ","+"),"gold",128);
        CheckBox chk;
        foreach (GridViewRow dr in grid_news.Rows)
        {
            chk = (CheckBox)dr.FindControl("chkGrant");
            try
            {
                var _tblgrant = _db.tblUserInRoles.Single(r => r.ID == int.Parse(grid_news.DataKeys[dr.RowIndex].Value.ToString()));
                _tblgrant.PhanQuyen = chk.Checked;
            }
            catch { }
        }
        _db.SubmitChanges();
        lblmessage.Text = "Đã phân quyền!";
    }
    //chọn all checkbox
    protected void chkselectAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in grid_news.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkGrant");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
}